<template>
    <div id="innerStruct" :class="drag?'drag':''">
        <div class="image-descs flex-center s-16" v-if="imageDescs">{{imageDescs[currentImg]}}</div>
        <div class="img-container flex-center">
            <img :src="images[currentImg]" alt="加载失败">
        </div>
        <div class="img-pages flex-between">
            <div class="page-change-arrows flex-start">
                <div class="change page-left flex-center pointer" @click="prevImg">
                    <img src="../../assets/img/page-left.png" alt="加载失败">
                </div>
                <div class="change page-right flex-center pointer" @click="nextImg">
                    <img src="../../assets/img/page-right.png" alt="加载失败">
                </div>
            </div>
            <div class="page-rect-handle flex-between">
                <div v-for="(item, index) in images" :key="index" class="dot-container">
                    <div :class="[currentImg===index ? 'current-page' : 'other-pages','pointer']" ref="changeHandle" @click="changeImgToThisIndex(index)" @touchstart="(e)=>dragCurrentImgByTouch(e,index)" @mousedown="(e)=>dragCurrentImg(e,index)"></div>
                    <!-- <div class="imageDescs-item" v-if="imageDescs">{{imageDescs[index]}}</div> -->
                </div>
            </div>
            <div class="page-change-arrows flex-start">
                <div class="change page-left flex-center pointer" @click="prevImg">
                    <img src="../../assets/img/page-left.png" alt="加载失败">
                </div>
                <div class="change page-right flex-center pointer" @click="nextImg">
                    <img src="../../assets/img/page-right.png" alt="加载失败">
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import mixin from '../../mixin/mixin'
export default {
    mixins: [mixin],
    data() {
        return {
            images: [],
            currentImg: 0,
            x: 0,
            drag: false,
            imageDescs: null,
        }
    },
    name: "InnerStruct",
    mounted() {
        if (this.bsr.imageDescs) {
            this.imageDescs = this.bsr.imageDescs.replace(/\s/g,'').split('|')
        }
        let imgArry = this.bsr.images.split('|')
        imgArry.forEach(v => {
            let suffix = this.getFileSuffix(v)
            this.images.push(window.$DATADIR + `resc/InnerStruct/${this.bsr.id}/${v.replace(/\.(?=[a-z])[a-z]{2,3}[a-z0-9]$/i, suffix)}`)
            this.pages = this.images.length - 1
        })
        console.log(this.images)
    },
    methods: {
        dragCurrentImgByTouch(e, index) {
            this.setInitPosition(e, index)
            document.documentElement.addEventListener('touchmove', this.mouseMove)
            document.documentElement.addEventListener('touchend', this.release)
        },
        dragCurrentImg(e, index) {
            this.setInitPosition(e, index)
            document.documentElement.addEventListener('pointermove', this.mouseMove)
            document.documentElement.addEventListener('mouseup', this.release)
        },
        setInitPosition(e, index) {
            let clientX = e.clientX ? e.clientX : e.changedTouches[0].clientX
            if (index !== this.currentImg) return
            this.x = clientX
            this.oldCurrent = this.currentImg
            this.width = this.$refs.changeHandle[1].getBoundingClientRect().x - this.$refs.changeHandle[0].getBoundingClientRect().x
            this.drag = true
        },
        mouseMove(e) {
            let clientX = e.clientX ? e.clientX : e.changedTouches[0].clientX
            console.log(clientX)
            if (clientX - this.x >= this.width / 2) {
                if (this.currentImg < this.images.length - 1) {
                    ++this.currentImg
                    this.x += this.width
                }
                // let index = this.oldCurrent + Math.floor((clientX - this.x) / this.width)
                // this.currentImg = index >= this.images.length - 1 ? this.images.length - 1 : index
            } else if (this.x - clientX >= this.width / 2) {
                if (this.currentImg > 0) {
                    --this.currentImg
                    this.x -= this.width
                }
                // let index = this.oldCurrent - Math.floor((this.x - clientX) / this.width)
                // this.currentImg = index <= 0 ? 0 : index
            }
        },
        release() {
            document.documentElement.removeEventListener('pointermove', this.mouseMove)
            document.documentElement.removeEventListener('touchmove', this.mouseMove)
            this.x = 0
            this.drag = false
        },
        changeImgToThisIndex(index) {
            this.currentImg = index
        },
        prevImg() {
            if (this.currentImg <= 0) return
            --this.currentImg
        },
        nextImg() {
            if (this.currentImg >= this.images.length - 1) return
            ++this.currentImg
        }
    },
}
</script>

<style lang="scss">
#innerStruct {
    position: relative;
    &.drag {
        * {
            cursor: pointer;
        }
    }
    .img-container {
        width: 100%;
        height: calc(100vh - 80px);
        img {
            height: 100%;
            vertical-align: bottom;
        }
    }
    .image-descs {
        position: absolute;
        width: 200px;
        height: 40px;
        background: rgba(0,0,0,.7);
        border-radius: 0px 0px 8px 0px;
        top: 0;
        left: 0;
        color: #FFF;
        z-index: 9999;
    }
    .img-pages {
        max-width: 100vw;
        height: 50px;
        background: #dcdfe7;
        padding: 0 20px;
        .page-rect-handle {
            flex: 1;
            height: 6px;
            background: #bfc6d9;
            .dot-container {
                position: relative;
                height: 44px;
                width: 18px;
                display: flex;
                align-items: center;
                justify-content: flex-start;
                &:last-child {
                    justify-content: flex-end;
                }
            }
        }

        .current-page {
            width: 16px;
            height: 16px;
            background: #4894ff;
            box-shadow: 0px 8px 13px 0px rgba(72, 148, 255, 0.16);
            border-radius: 50%;
        }
        .other-pages {
            width: 12px;
            height: 12px;
            background: rgba(191, 198, 217, 1);
            border-radius: 50%;
        }
        .change {
            width: 32px;
            height: 32px;
            background: rgba(220, 223, 231, 1);
            border: 1px solid rgba(72, 148, 255, 1);
            img {
                width: 11px;
                height: 11px;
            }
        }
        .page-change-arrows {
            width: 64px;
            &:nth-child(1) {
                margin-right: 27px;
            }
            &:nth-child(3) {
                margin-left: 27px;
            }
        }

        .page-left {
            border-radius: 8px 0px 0px 8px;
        }
        .page-right {
            border-radius: 0px 8px 8px 0px;
            border-left: 1px solid rgba(72, 148, 255, 1);
        }
    }
}
</style>
